Read in data

Glasser regions and assignments

#Glasser region and label names for the frontal lobe
glasser.regions <- read.csv("/Volumes/Hera/Projects/corticalmyelin_development/Maps/HCPMMP_glasseratlas/glasser360_regionlist.csv")
glasser.frontal <- read.csv("/Volumes/Hera/Projects/corticalmyelin_development/Maps/HCPMMP_glasseratlas/glasser360_regionlist_frontallobe.csv")
glasser.snr.exclude <- read.csv("/Volumes/Hera/Projects/corticalmyelin_development/Maps/SNR/glasser_SNR_exclusion.csv")
glasser.plotting <- glasser.regions
glasser.plotting$cortex <- "cortex"
glasser.plotting$cortex[glasser.plotting$orig_parcelname %in% glasser.snr.exclude$orig_parcelname] <- "exclude"
glasser.plotting <- glasser.plotting %>% select(label, cortex)
glasser.plotting <- rbind(glasser.plotting, data.frame(label = "rh_???", cortex = "exclude"))
glasser.plotting <- rbind(glasser.plotting, data.frame(label = "lh_???", cortex = "exclude"))
glasser.plotting <- glasser.plotting %>% filter(label != "lh_L_10pp")

Depths

depth.list <- c("depth_1", "depth_2", "depth_3", "depth_4", "depth_5", "depth_6", "depth_7")
ordered_depths <- c("depth_7", "depth_6", "depth_5", "depth_4", "depth_3", "depth_2", "depth_1")
depth_colorbar <-  c("#004A38FF", "#006B63FF", "#008FA7FF", "#6992CC", "#A29DC4", "#C0BFE3", "#E2CFE5")
depth_colorbar_reverse <-  c("#E2CFE5", "#C0BFE3", "#A29DC4", "#6992CC", "#008FA7FF", "#006B63FF", "#004A38FF")

S-A Axis

#S-A axis or saxis as nicknamed by Dan Margulies on a trip to gingerworld
S.A.axis.cifti <- read_cifti("/Volumes/Hera/Projects/corticalmyelin_development/Maps/S-A_ArchetypalAxis/FSLRVertex/SensorimotorAssociation_Axis_parcellated/SensorimotorAssociation.Axis.Glasser360.pscalar.nii") #S-A_ArchetypalAxis repo
S.A.axis <- data.frame(SA.axis = rank(S.A.axis.cifti$data), orig_parcelname = names(S.A.axis.cifti$Parcel))
S.A.axis <- merge(S.A.axis, glasser.regions, by = "orig_parcelname")

BigBrain Histology Gradient

#BigBrain histology-based cytoarchitecture differentiation gradient from Paquola et al., 2021, eLife
bigbrain.cytoarchitecture.cifti <- read_cifti("/Volumes/Hera/Projects/corticalmyelin_development/Maps/BigBrain_histologygradient/BigBrain.Histology.pscalar.nii")
bigbrain <- data.frame(cytoarchitecture.gradient = bigbrain.cytoarchitecture.cifti$data, orig_parcelname = names(bigbrain.cytoarchitecture.cifti$Parcel))
bigbrain <- merge(bigbrain, glasser.regions, by = "orig_parcelname")

Neurosynth Term Scores

neurosynth.terms <- read.csv("/Volumes/Hera/Projects/corticalmyelin_development/Maps/Neurosynth_terms/atl-glasser360_neurosynth.csv") %>% dplyr::select(-regionName) %>% dplyr::select(-timing) #neurosynth term meta-analytic scores for 123 terms present in both NeuroSynth and the Cognitive Atlas, ordered in lh --> rh glasser order
neurosynth.termlist <- names(neurosynth.terms) #list of term names
neurosynth.terms$label[1:180] <- glasser.regions$label[181:360] #lh first
neurosynth.terms$label[181:360] <- glasser.regions$label[1:180] #rh
neurosynth.terms[,1:123] <- scale(neurosynth.terms[,1:123])

Final participant list

participants <- read.csv("/Volumes/Hera/Projects/corticalmyelin_development/sample_info/7T_MP2RAGE_finalsample_demographics.csv")
participants <- participants %>% mutate(subses = sprintf("%s_%s", subject_id, session_id))

Depth-dependent R1 in HCP-MMP regions

#R1 by region matrices at 7 cortical depths
myelin.glasser.7T <- readRDS("/Volumes/Hera/Projects/corticalmyelin_development/BIDS/derivatives/surface_metrics/depthR1_glasseratlas_finalsample.RDS") #generated by /surface_metrics/surface_measures/extract_depthdependent_R1.R

GAM outputs: developmental effects

setwd("/Volumes/Hera/Projects/corticalmyelin_development/gam_outputs/developmental_effects/") #output from /gam_models/fitgams_glasserregions_bydepth.R

files <- list.files(getwd(), pattern = "age.RDS") 

#read in files and assign to variables
for(i in 1:length(files)){
  
  Rfilename <- gsub(".RDS", "", files[i])
  
  x <- readRDS(files[i]) 
  assign(Rfilename, x) 
  }
#Combine all outputs stats. A list of lists! #inception #spinning top
#7 depths included, each depth contains 4 dfs (stats, fitted, smooths, derivatives)
gam.results.alldepths <- list(depth1_gamstatistics_age, depth2_gamstatistics_age, depth3_gamstatistics_age, depth4_gamstatistics_age, depth5_gamstatistics_age, depth6_gamstatistics_age, depth7_gamstatistics_age)
names(gam.results.alldepths) <- list("depth_1", "depth_2", "depth_3", "depth_4", "depth_5", "depth_6", "depth_7")
#Extract the 4 types of results and format
gam.statistics.alldepths <- lapply(gam.results.alldepths, '[[', "gam.statistics.df" )
gam.statistics.allregions <- gam.statistics.alldepths #whole brain data
gam.fittedvalues.alldepths <-  lapply(gam.results.alldepths, '[[', "gam.fittedvalues.df" )
gam.smoothestimates.alldepths <- lapply(gam.results.alldepths, '[[', "gam.smoothestimates.df" )
gam.smoothestimates.allregions <- lapply(gam.results.alldepths, '[[', "gam.smoothestimates.df" ) #wholebrain data
gam.derivatives.alldepths <- lapply(gam.results.alldepths, '[[', "gam.derivatives.df" )

format_depth_dfs <- function(depth){
  depth <- depth[depth$orig_parcelname %in% glasser.frontal$orig_parcelname,] #get results for frontal lobe only
  depth <-  depth[!(depth$orig_parcelname %in% glasser.snr.exclude$orig_parcelname),] #exclude low SNR parcels
}

gam.statistics.alldepths <- lapply(gam.statistics.alldepths, function(depth){
  format_depth_dfs(depth)
})
gam.fittedvalues.alldepths <- lapply(gam.fittedvalues.alldepths, function(depth){
  format_depth_dfs(depth)
})
gam.smoothestimates.alldepths <- lapply(gam.smoothestimates.alldepths, function(depth){
  format_depth_dfs(depth)
})
gam.derivatives.alldepths <- lapply(gam.derivatives.alldepths, function(depth){
  format_depth_dfs(depth)
})

format_region_dfs <- function(depth){
  depth <-  depth[!(depth$orig_parcelname %in% glasser.snr.exclude$orig_parcelname),] #exclude low SNR parcels
}

gam.statistics.allregions <- lapply(gam.statistics.allregions, function(depth){
  format_region_dfs(depth)
})

gam.smoothestimates.allregions <- lapply(gam.smoothestimates.allregions, function(depth){
  format_region_dfs(depth)
})

Frontal Myelin Matures Heterochronously Across Cortical Depths

Frontal lobe developmental trajectory at each depth

#Long formatted R1 data for all scans + frontal lobe regions at each depth
myelin.glasser.7T.long <- lapply(myelin.glasser.7T, function(depth){
  cols_to_pivot <- names(depth)[grepl("ROI", names(depth))]
  depth.long <- depth %>% pivot_longer(cols = cols_to_pivot, names_to = "orig_parcelname", values_to = "R1")
  depth.long <- merge(depth.long, glasser.frontal, by = "orig_parcelname")
  depth.long <- depth.long[!(depth.long$orig_parcelname %in% glasser.snr.exclude$orig_parcelname),]
  depth.long <- depth.long %>% mutate(subses = sprintf("%s_%s", subject_id, session_id))
  return(depth.long)
})
#Calculate average frontal lobe R1 for each sub/ses
myelin.frontallobe.7T <- lapply(myelin.glasser.7T.long, function(depth){
    depth <- depth %>% group_by(subses) %>% do(mean.R1 = mean(.$R1)) %>% unnest(cols = c( "mean.R1"))
    depth <- merge(depth, participants, by = "subses")
    depth$subject_id <- as.factor(depth$subject_id)
    depth$sex <- as.factor(depth$sex)
    return(depth)
})
#Function to fit a frontal lobe gam for a given depth and return model summary
gam.summary <- function(depth){
  
  depth.data <- depth
  
  model <- gam(mean.R1 ~ s(age, k = 4, fx = F) + s(subject_id, bs = "re"), method = c("REML"), data = depth.data)
  
  print(summary(model))
}
#Function to fit a frontal lobe gam for a given depth and plot the developmental trajectory
plot.trajectory <- function(depth, display_color, y_lims, y_ticks){
  
  depth.data <- depth
  
  #First fit the gam and get fitted values and derivatives 
  depth.model <- gam.statistics.smooths(input.df = depth.data, region = "mean.R1", smooth_var = "age", id_var = "subject_id", covariates = "NA", random_intercepts = T, random_slopes = F, knots = 4, set_fx = F)

  #Age spline plot with participant-level data
  trajectory.plot <- ggplot() +
    geom_point(data = depth.data, aes(x = age, y = mean.R1, group = subject_id), color = "black", size = .4) +
    geom_line(data = depth.data, aes(x = age, y = mean.R1, group = subject_id), linewidth = 0.3, color = "gray75") +
    geom_line(data = depth.model$gam.fittedvalues, aes(x = age, y = fitted), color = display_color, linewidth = 1.5) +
    labs(x="\nAge", y=sprintf("R1\n")) +
    theme_classic() +
    scale_y_continuous(limits = y_lims, breaks = y_ticks) + 
    theme(
        legend.position = "none", 
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        axis.text = element_text(size = 6, family = "Arial", color = c("black")),
        axis.title.x = element_text(size = 7, family ="Arial", color = c("black")),
        axis.title.y = element_text(size = 7, family ="Arial", color = c("black")),
        axis.line = element_line(linewidth = .2), 
        axis.ticks = element_line(linewidth = .2))
  
  return(trajectory.plot)
}
#Function to fit a frontal lobe gam for a given depth and return the fitted values df
depth.fittedvalues <- function(depth){
  
  depth.data <- depth
  
  #First fit the gam and get fitted values
  depth.model <- gam.statistics.smooths(input.df = depth.data, region = "mean.R1", smooth_var = "age", id_var = "subject_id", covariates = "NA", random_intercepts = T, random_slopes = F, knots = 4, set_fx = F)
  
  depth.fitted <- depth.model$gam.fittedvalues
  return(depth.fitted)
}

Depth-speficic plots

Depth 1

gam.summary(myelin.frontallobe.7T$depth_1)
## 
## Family: gaussian 
## Link function: identity 
## 
## Formula:
## mean.R1 ~ s(age, k = 4, fx = F) + s(subject_id, bs = "re")
## 
## Parametric coefficients:
##              Estimate Std. Error t value Pr(>|t|)    
## (Intercept) 0.5145777  0.0008383   613.8   <2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Approximate significance of smooth terms:
##                  edf  Ref.df      F  p-value    
## s(age)         1.236   1.357 24.720 4.30e-07 ***
## s(subject_id) 67.102 139.000  1.049 3.25e-06 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## R-sq.(adj) =  0.476   Deviance explained = 64.4%
## -REML = -663.39  Scale est. = 7.0085e-05  n = 215
plot.trajectory(depth = myelin.frontallobe.7T$depth_1, display_color = depth_colorbar[7], y_lims = c(0.48, 0.555), y_ticks  = c(0.49, 0.52, 0.55))

ggsave(filename = "/Volumes/Hera/Projects/corticalmyelin_development/Figures/Figure2/Depth1_trajectoryplot.pdf", device = "pdf", dpi = 500, width = 2.48, height = 1.48)

Depth 2

gam.summary(myelin.frontallobe.7T$depth_2)
## 
## Family: gaussian 
## Link function: identity 
## 
## Formula:
## mean.R1 ~ s(age, k = 4, fx = F) + s(subject_id, bs = "re")
## 
## Parametric coefficients:
##              Estimate Std. Error t value Pr(>|t|)    
## (Intercept) 0.5261190  0.0008012   656.7   <2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Approximate significance of smooth terms:
##                  edf  Ref.df      F  p-value    
## s(age)         1.943   2.216 31.910  < 2e-16 ***
## s(subject_id) 62.903 139.000  0.916 1.69e-05 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## R-sq.(adj) =  0.525   Deviance explained = 66.9%
## -REML = -670.04  Scale est. = 6.7933e-05  n = 215
plot.trajectory(depth = myelin.frontallobe.7T$depth_2, display_color = depth_colorbar[6], y_lims = c(0.495, 0.56), y_ticks = c(0.50, 0.53, 0.56))

ggsave(filename = "/Volumes/Hera/Projects/corticalmyelin_development/Figures/Figure2/Depth2_trajectoryplot.pdf", device = "pdf", dpi = 500, width = 2.48, height = 1.48)

Depth 3

gam.summary(myelin.frontallobe.7T$depth_3)
## 
## Family: gaussian 
## Link function: identity 
## 
## Formula:
## mean.R1 ~ s(age, k = 4, fx = F) + s(subject_id, bs = "re")
## 
## Parametric coefficients:
##              Estimate Std. Error t value Pr(>|t|)    
## (Intercept) 0.5364280  0.0007889     680   <2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Approximate significance of smooth terms:
##                  edf  Ref.df      F  p-value    
## s(age)         2.199   2.473 42.504  < 2e-16 ***
## s(subject_id) 60.074 139.000  0.833 5.04e-05 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## R-sq.(adj) =   0.57   Deviance explained = 69.5%
## -REML = -671.36  Scale est. = 6.8544e-05  n = 215
plot.trajectory(depth = myelin.frontallobe.7T$depth_3, display_color = depth_colorbar[5], y_lims = c(0.505, 0.57), y_ticks = c(0.51, 0.54, 0.57))

ggsave(filename = "/Volumes/Hera/Projects/corticalmyelin_development/Figures/Figure2/Depth3_trajectoryplot.pdf", device = "pdf", dpi = 500, width = 2.48, height = 1.48)

Depth 4

gam.summary(myelin.frontallobe.7T$depth_4)
## 
## Family: gaussian 
## Link function: identity 
## 
## Formula:
## mean.R1 ~ s(age, k = 4, fx = F) + s(subject_id, bs = "re")
## 
## Parametric coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept) 0.546799   0.000796   686.9   <2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Approximate significance of smooth terms:
##                  edf  Ref.df      F  p-value    
## s(age)         2.325   2.591 51.691  < 2e-16 ***
## s(subject_id) 57.575 139.000  0.768 0.000121 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## R-sq.(adj) =    0.6   Deviance explained = 71.2%
## -REML = -667.8  Scale est. = 7.226e-05  n = 215
plot.trajectory(depth = myelin.frontallobe.7T$depth_4, display_color = depth_colorbar[4], y_lims = c(0.51, 0.585), y_ticks = c(0.52, 0.55, 0.58))

ggsave(filename = "/Volumes/Hera/Projects/corticalmyelin_development/Figures/Figure2/Depth4_trajectoryplot.pdf", device = "pdf", dpi = 500, width = 2.48, height = 1.48)

Depth 5

gam.summary(myelin.frontallobe.7T$depth_5)
## 
## Family: gaussian 
## Link function: identity 
## 
## Formula:
## mean.R1 ~ s(age, k = 4, fx = F) + s(subject_id, bs = "re")
## 
## Parametric coefficients:
##              Estimate Std. Error t value Pr(>|t|)    
## (Intercept) 0.5596640  0.0008176   684.5   <2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Approximate significance of smooth terms:
##                 edf  Ref.df      F  p-value    
## s(age)         2.39   2.649 58.508  < 2e-16 ***
## s(subject_id) 55.42 139.000  0.716 0.000238 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## R-sq.(adj) =  0.618   Deviance explained = 72.1%
## -REML = -660.76  Scale est. = 7.8505e-05  n = 215
plot.trajectory(depth = myelin.frontallobe.7T$depth_5, display_color = depth_colorbar[3], y_lims = c(0.525, 0.6), y_ticks = c(0.53, 0.56, 0.59))

ggsave(filename = "/Volumes/Hera/Projects/corticalmyelin_development/Figures/Figure2/Depth5_trajectoryplot.pdf", device = "pdf", dpi = 500, width = 2.48, height = 1.48)

Depth 6

gam.summary(myelin.frontallobe.7T$depth_6)
## 
## Family: gaussian 
## Link function: identity 
## 
## Formula:
## mean.R1 ~ s(age, k = 4, fx = F) + s(subject_id, bs = "re")
## 
## Parametric coefficients:
##              Estimate Std. Error t value Pr(>|t|)    
## (Intercept) 0.5779681  0.0008437   685.1   <2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Approximate significance of smooth terms:
##                  edf  Ref.df      F  p-value    
## s(age)         2.414   2.669 62.514  < 2e-16 ***
## s(subject_id) 54.658 139.000  0.698 0.000304 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## R-sq.(adj) =  0.629   Deviance explained = 72.8%
## -REML = -653.6  Scale est. = 8.4454e-05  n = 215
plot.trajectory(depth = myelin.frontallobe.7T$depth_6, display_color = depth_colorbar[2], y_lims = c(0.54, 0.63), y_ticks = c(0.56, 0.59, 0.62))

ggsave(filename = "/Volumes/Hera/Projects/corticalmyelin_development/Figures/Figure2/Depth6_trajectoryplot.pdf", device = "pdf", dpi = 500, width = 2.48, height = 1.48)

Depth 7

gam.summary(myelin.frontallobe.7T$depth_7)
## 
## Family: gaussian 
## Link function: identity 
## 
## Formula:
## mean.R1 ~ s(age, k = 4, fx = F) + s(subject_id, bs = "re")
## 
## Parametric coefficients:
##              Estimate Std. Error t value Pr(>|t|)    
## (Intercept) 0.6041477  0.0008685   695.6   <2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Approximate significance of smooth terms:
##                 edf  Ref.df      F p-value    
## s(age)         2.40   2.654 62.389 < 2e-16 ***
## s(subject_id) 57.61 139.000  0.764 0.00014 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## R-sq.(adj) =  0.641   Deviance explained = 74.2%
## -REML = -649.14  Scale est. = 8.5938e-05  n = 215
plot.trajectory(depth = myelin.frontallobe.7T$depth_7, display_color = depth_colorbar[1], y_lims = c(0.565, 0.65), y_ticks = c(0.58, 0.61, 0.64))

ggsave(filename = "/Volumes/Hera/Projects/corticalmyelin_development/Figures/Figure2/Depth7_trajectoryplot.pdf", device = "pdf", dpi = 500, width = 2.48, height = 1.48)
ps <- c(4.30e-07, 2e-16, 2e-16, 2e-16, 2e-16, 2e-16, 2e-16)
p.adjust(ps, method = c("fdr"))
## [1] 4.300000e-07 2.333333e-16 2.333333e-16 2.333333e-16 2.333333e-16
## [6] 2.333333e-16 2.333333e-16

Multi-depth plots

frontal.fittedvalues.alldepths <- lapply(myelin.frontallobe.7T, function(depth){
  depth.fittedvalues(depth)
})
ggplot() +
#depth 1
geom_point(data = myelin.frontallobe.7T$depth_1, aes(x = age, y = mean.R1, group = subject_id), color = "black", size = .5) +
geom_line(data = myelin.frontallobe.7T$depth_1, aes(x = age, y = mean.R1, group = subject_id), linewidth = 0.3, color = "gray75") +
geom_line(data = frontal.fittedvalues.alldepths$depth_1, aes(x = age, y = fitted), color = depth_colorbar[7], linewidth = 1.8) +
#depth 7
geom_point(data = myelin.frontallobe.7T$depth_7, aes(x = age, y = mean.R1, group = subject_id), color = "black", size = .5) +
geom_line(data = myelin.frontallobe.7T$depth_7, aes(x = age, y = mean.R1, group = subject_id), linewidth = 0.3, color = "gray75") +
geom_line(data = frontal.fittedvalues.alldepths$depth_7, aes(x = age, y = fitted), color = depth_colorbar[1], linewidth = 1.8) +
labs(x="\nAge", y=sprintf("R1\n")) +
theme_classic() +
scale_y_continuous(limits = c(0.48, 0.65)) +
theme(
  legend.position = "none", 
  panel.grid.major = element_blank(),
  panel.grid.minor = element_blank(),
  axis.text = element_text(size = 6, family = "Arial", color = c("black")),
  axis.title.x = element_text(size = 7, family ="Arial", color = c("black")),
  axis.title.y = element_text(size = 7, family ="Arial", color = c("black")),
  axis.line = element_line(linewidth = .2), 
  axis.ticks = element_line(linewidth = .2))

ggsave(filename = "/Volumes/Hera/Projects/corticalmyelin_development/Figures/Figure2_supplementarya/Depth_trajectories_withdata.pdf", device = "pdf", dpi = 500, width = 2.7, height = 4.65)
ggplot() +
#depth 1
geom_line(data = frontal.fittedvalues.alldepths$depth_1, aes(x = age, y = fitted), color = depth_colorbar[7], linewidth = 1.8) +
#depth 2
geom_line(data = frontal.fittedvalues.alldepths$depth_2, aes(x = age, y = fitted), color = depth_colorbar[6], linewidth = 1.8) +
#depth 3
geom_line(data = frontal.fittedvalues.alldepths$depth_3, aes(x = age, y = fitted), color = depth_colorbar[5], linewidth = 1.8) +
#depth 4
geom_line(data = frontal.fittedvalues.alldepths$depth_4, aes(x = age, y = fitted), color = depth_colorbar[4], linewidth = 1.8) +
#depth 5
geom_line(data = frontal.fittedvalues.alldepths$depth_5, aes(x = age, y = fitted), color = depth_colorbar[3], linewidth = 1.8) +
#depth 6
geom_line(data = frontal.fittedvalues.alldepths$depth_6, aes(x = age, y = fitted), color = depth_colorbar[2], linewidth = 1.8) +
#depth 7
geom_line(data = frontal.fittedvalues.alldepths$depth_7, aes(x = age, y = fitted), color = depth_colorbar[1], linewidth = 1.8) +
labs(x="\nAge", y=sprintf("R1\n")) +
theme_classic() +
scale_y_continuous(limits = c(0.48, 0.65)) +
theme(
  legend.position = "none", 
  panel.grid.major = element_blank(),
  panel.grid.minor = element_blank(),
  axis.text = element_text(size = 6, family = "Arial", color = c("black")),
  axis.title.x = element_text(size = 7, family ="Arial", color = c("black")),
  axis.title.y = element_text(size = 7, family ="Arial", color = c("black")),
  axis.line = element_line(linewidth = .2), 
  axis.ticks = element_line(linewidth = .2))

ggsave(filename = "/Volumes/Hera/Projects/corticalmyelin_development/Figures/Figure2_supplementarya/Depth_trajectories_allfits.pdf", device = "pdf", dpi = 500, width = 2.7, height = 4.65)

Age-by-depth interaction

myelin.frontallobe.alldepths <- do.call(rbind, myelin.frontallobe.7T)
myelin.frontallobe.alldepths <- myelin.frontallobe.alldepths %>% mutate(depth.factor = as.factor(substr(row.names(myelin.frontallobe.alldepths), 1, 7)))
myelin.frontallobe.alldepths <- myelin.frontallobe.alldepths %>% mutate(depth = as.numeric(substr(row.names(myelin.frontallobe.alldepths), 7, 7)))

summary(gam(mean.R1 ~ s(age, k = 3, fx = F) + s(depth, k = 5, fx = F) + te(age, depth, k = c(3, 5)) + s(subject_id, by = depth.factor, bs = "re"), method = "REML", data = myelin.frontallobe.alldepths))
## 
## Family: gaussian 
## Link function: identity 
## 
## Formula:
## mean.R1 ~ s(age, k = 3, fx = F) + s(depth, k = 5, fx = F) + te(age, 
##     depth, k = c(3, 5)) + s(subject_id, by = depth.factor, bs = "re")
## 
## Parametric coefficients:
##              Estimate Std. Error t value Pr(>|t|)    
## (Intercept) 0.5522469  0.0003096    1784   <2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Approximate significance of smooth terms:
##                                      edf  Ref.df        F  p-value    
## s(age)                             1.981   1.993  119.522  < 2e-16 ***
## s(depth)                           2.636   2.755 1699.151  < 2e-16 ***
## te(age,depth)                      4.048  11.000   14.689  < 2e-16 ***
## s(subject_id):depth.factordepth_1 62.923 140.000    0.920  < 2e-16 ***
## s(subject_id):depth.factordepth_2 55.148 140.000    0.717  < 2e-16 ***
## s(subject_id):depth.factordepth_3 52.382 140.000    0.651 3.70e-06 ***
## s(subject_id):depth.factordepth_4 53.825 140.000    0.677 2.36e-06 ***
## s(subject_id):depth.factordepth_5 58.056 140.000    0.766  < 2e-16 ***
## s(subject_id):depth.factordepth_6 63.247 140.000    0.894  < 2e-16 ***
## s(subject_id):depth.factordepth_7 67.080 140.000    1.000  < 2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## R-sq.(adj) =  0.925   Deviance explained = 94.6%
## -REML = -4695.5  Scale est. = 7.5445e-05  n = 1505

Robust longitudinal increases in frontal lobe R1

#Compute longitudinal change in frontal lobe R1 between paired longitudinal sessions, at each cortical depth
compute_R1_longitudinalchange <- function(input.df){
  
  input.df$mp2rage.date <- ymd(input.df$mp2rage.date)
  
  #session-to-session raw longitudinal change in R1
  R1.longitudinal.change <- input.df %>% group_by(subject_id) %>%
    mutate(R1.change.raw = mean.R1 - lag(mean.R1)) %>% 
    ungroup() %>% select(subject_id, session_id, age, mp2rage.session_number, mean.R1, R1.change.raw)
  
  #session-to-session longitudinal change in age
  R1.longitudinal.change <- R1.longitudinal.change %>% group_by(subject_id) %>%
    mutate(age.change = age - lag(age)) %>% 
    ungroup() %>% select(subject_id, session_id, age, mp2rage.session_number, mean.R1, R1.change.raw, age.change)
  
  #midpoint between-session age
  R1.longitudinal.change <- R1.longitudinal.change %>% group_by(subject_id) %>%
    mutate(mean.age = (age + lag(age))/2) %>% 
    ungroup() %>% select(subject_id, session_id, age, mp2rage.session_number, mean.R1, R1.change.raw, age.change, mean.age)
  
  #longitudinal change in R1 scaled by change in age (delta R1/ delta age = R1 change slope!)
  R1.longitudinal.change <- R1.longitudinal.change %>% mutate(R1.change.slope = R1.change.raw/age.change)
  
  #remove subjects with a single timepoint only
  R1.longitudinal.change <- R1.longitudinal.change %>% group_by(subject_id) %>% filter(n() > 1) %>% ungroup() 
}

myelin.frontallobe.longchange <- lapply(myelin.frontallobe.7T, function(depth){
  compute_R1_longitudinalchange(input.df = depth)
})
# >75% of longitudinal visits show an increase in frontal cortex R1 at each depth, with an average of 80.2% across depths
lapply(myelin.frontallobe.longchange, function(depth){
  R1.longincrease.percent <- round((sum(depth$R1.change.raw > 0, na.rm = T) / sum(!(is.na(depth$R1.change.raw))))*100, 2)
  sprintf("%s percent of longitudinal visits show an increase in R1", R1.longincrease.percent)
})
## $depth_1
## [1] "76 percent of longitudinal visits show an increase in R1"
## 
## $depth_2
## [1] "81.33 percent of longitudinal visits show an increase in R1"
## 
## $depth_3
## [1] "82.67 percent of longitudinal visits show an increase in R1"
## 
## $depth_4
## [1] "82.67 percent of longitudinal visits show an increase in R1"
## 
## $depth_5
## [1] "80 percent of longitudinal visits show an increase in R1"
## 
## $depth_6
## [1] "78.67 percent of longitudinal visits show an increase in R1"
## 
## $depth_7
## [1] "80 percent of longitudinal visits show an increase in R1"

Within-individual longitudinal slopes at exemplar depths

#Plot within-person R1 + slope at younger and older scans 
myelin.frontallobe.longchange.alldepths <- do.call(rbind, myelin.frontallobe.longchange) #long df with all depths
myelin.frontallobe.longchange.alldepths <- myelin.frontallobe.longchange.alldepths %>% mutate(depth = as.factor(substr(row.names(myelin.frontallobe.longchange.alldepths), 1, 7))) 
myelin.frontallobe.longchange.alldepths$depth <- factor(myelin.frontallobe.longchange.alldepths$depth, ordered = T, levels = ordered_depths)

scans.t1t2 <- myelin.frontallobe.longchange.alldepths %>% filter(depth == "depth_1" | depth == "depth_5" | depth ==  "depth_7") %>% filter(mp2rage.session_number < 3) #first get paired visits 1 and 2
scans.t1t2$mp2rage.session_number[scans.t1t2$mp2rage.session_number == 1] <- "Younger" 
scans.t1t2$mp2rage.session_number[scans.t1t2$mp2rage.session_number == 2] <- "Older"
scans.t1t2$mp2rage.session_number <- factor(scans.t1t2$mp2rage.session_number, levels = c("Younger", "Older"))

scans.t2t3 <- myelin.frontallobe.longchange.alldepths %>% filter(depth == "depth_1" | depth == "depth_5" | depth ==  "depth_7") %>% filter(mp2rage.session_number > 1) #next get paired visits 2 and 3
scans.t2t3 <- scans.t2t3 %>% group_by(subject_id, depth) %>% filter(n() > 1) %>%  ungroup() # make sure we only plot paired data (no ridin solo)
scans.t2t3$mp2rage.session_number[scans.t2t3$mp2rage.session_number == 2] <- "Younger"
scans.t2t3$mp2rage.session_number[scans.t2t3$mp2rage.session_number == 3] <- "Older"
scans.t2t3$mp2rage.session_number <- factor(scans.t2t3$mp2rage.session_number, levels = c("Younger", "Older"))


ggplot() +
  geom_point(data = scans.t1t2, aes(x = mp2rage.session_number, y = mean.R1, color = depth)) +
  geom_line(data = scans.t1t2, aes(x = mp2rage.session_number, y = mean.R1, group = interaction(subject_id, depth), color = depth), linewidth = 0.25) + 
  geom_point(data = scans.t2t3, aes(x = mp2rage.session_number, y = mean.R1, color = depth)) +
  geom_line(data = scans.t2t3, aes(x = mp2rage.session_number, y = mean.R1, group = interaction(subject_id, depth), color = depth), linewidth = 0.25) + 
  theme_classic() +
  scale_x_discrete(expand = c(0.03, 0.03)) +
  scale_color_manual(values = c(alpha("#004A38FF", 0.7), alpha("#008FA7FF", 0.7), alpha("#E2CFE5", 0.7))) +
  xlab("\n") +
  ylab("Frontal Cortex R1\n") +
  theme(
  legend.position = "none",
  axis.text = element_text(size = 6, family = "Arial", color = c("black")),
  axis.title.x = element_text(size = 7, family ="Arial", color = c("black")),
  axis.title.y = element_text(size = 7, family ="Arial", color = c("black")),
  axis.line = element_line(linewidth = .2), 
  axis.ticks = element_line(linewidth = .2)) 

ggsave(filename = "/Volumes/Hera/Projects/corticalmyelin_development/Figures/Figure2_supplementaryb/Longitudinal_slopes.pdf", device = "pdf", dpi = 500, width = 2.65, height = 3.2)

R1 slopes (rate of change) at each cortical depths

All data: violin plots

myelin.frontallobe.longchange.alldepths %>% 
  ggplot(aes(x = R1.change.slope, y = depth, fill = depth)) +
  geom_violin(aes(fill = depth), color = "white") +
  geom_vline(xintercept = 0, linetype = "dashed") +
  theme_classic() +
  scale_fill_manual(values = depth_colorbar) +
  xlab("\nR1 Rate of Change (slope)") +
  ylab("Cortical Depth\n") +
  scale_x_continuous(limits = c(-0.008, 0.022)) +
  theme(
  legend.position = "none",
  axis.text = element_text(size = 6, family = "Arial", color = c("black")),
  axis.title.x = element_text(size = 7, family ="Arial", color = c("black")),
  axis.title.y = element_text(size = 7, family ="Arial", color = c("black")),
  axis.line = element_line(linewidth = .2), 
  axis.ticks = element_line(linewidth = .2)) 

ggsave(filename = "/Volumes/Hera/Projects/corticalmyelin_development/Figures/Figure2_supplementaryb/R1slope_depthplot_violin.pdf", device = "pdf", dpi = 500, width = 2.6, height = 3.2)

Depth means

R1age.slope.longitudinal <- lapply(myelin.frontallobe.longchange, function(depth){
  R1.age.slope <- mean(depth$R1.change.slope, na.rm = T)
  return(R1.age.slope)
})

R1age.slope.longitudinal <- do.call(rbind, R1age.slope.longitudinal) %>% as.data.frame() %>% set_names("R1.change.slope")
R1age.slope.longitudinal$depth <- factor(row.names(R1age.slope.longitudinal), ordered = T, levels = ordered_depths)

R1age.slope.longitudinal %>% 
  ggplot(aes(x = R1.change.slope, y = depth, fill = depth)) +
  geom_point(shape = 23, size = 3.4, color = "white") +
  theme_classic() +
  scale_fill_manual(values = depth_colorbar) +
  scale_x_continuous(limits = c(0.004, 0.0056)) +
  xlab("\nR1 Rate of Change (slope)") +
  ylab("Cortical Depth\n") +
  theme(
  legend.position = "none",
  axis.text = element_text(size = 6, family = "Arial", color = c("black")),
  axis.title.x = element_text(size = 7, family ="Arial", color = c("black")),
  axis.title.y = element_text(size = 7, family ="Arial", color = c("black")),
  axis.line = element_line(linewidth = .2), 
  axis.ticks = element_line(linewidth = .2)) 

ggsave(filename = "/Volumes/Hera/Projects/corticalmyelin_development/Figures/Figure2_supplementaryb/R1slope_depthplot_meanpoint.pdf", device = "pdf", dpi = 500, width = 2.15, height = 1.7)

Differences in within-individual rate of change by depth and age

myelin.frontallobe.longchange.alldepths$depth <- as.factor(gsub(".*?_", "", myelin.frontallobe.longchange.alldepths$depth)) 
myelin.frontallobe.longchange.alldepths <- myelin.frontallobe.longchange.alldepths %>% filter(!is.na(R1.change.slope)) #75 longitudinal visits for 7 depths
myelin.frontallobe.longchange.alldepths <- as.data.frame(myelin.frontallobe.longchange.alldepths)

summary(gam(R1.change.slope ~ s(mean.age, k = 4, fx = F) + s(depth, bs = "re"), method = c("REML"), data = myelin.frontallobe.longchange.alldepths))
## 
## Family: gaussian 
## Link function: identity 
## 
## Formula:
## R1.change.slope ~ s(mean.age, k = 4, fx = F) + s(depth, bs = "re")
## 
## Parametric coefficients:
##              Estimate Std. Error t value Pr(>|t|)    
## (Intercept) 0.0048353  0.0002682   18.03   <2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Approximate significance of smooth terms:
##                  edf Ref.df     F p-value  
## s(mean.age) 1.986979  2.359 4.322  0.0119 *
## s(depth)    0.003484  6.000 0.000  0.7500  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## R-sq.(adj) =  0.0167   Deviance explained = 2.05%
## -REML =  -1914  Scale est. = 3.7753e-05  n = 525
R1rate.by.age.model <- gam.statistics.smooths(input.df = myelin.frontallobe.longchange.alldepths, region = "R1.change.slope", smooth_var = "mean.age", id_var = "depth", covariates = "NA", random_intercepts = TRUE, random_slopes = FALSE, knots = 4, set_fx = FALSE)

#Developmental window of deceleration
R1rate.by.age.model$gam.derivatives %>% filter(significant == TRUE) %>% select(mean.age) %>% min()
## [1] 15.41405
R1rate.by.age.model$gam.derivatives %>% filter(significant == TRUE) %>% select(mean.age) %>% max()
## [1] 18.72242
R1rate.by.age.model$gam.fittedvalues$sig.deriv <- (R1rate.by.age.model$gam.fittedvalues$mean.age > 15.4 & R1rate.by.age.model$gam.fittedvalues$mean.age < 18.7)
R1rate.by.age.model$gam.fittedvalues$sig.deriv.ages <- R1rate.by.age.model$gam.fittedvalues$mean.age*R1rate.by.age.model$gam.fittedvalues$sig.deriv
R1rate.by.age.model$gam.fittedvalues$sig.deriv.ages[R1rate.by.age.model$gam.fittedvalues$sig.deriv.ages == 0] <- NA

ggplot(R1rate.by.age.model$gam.fittedvalues, aes(x = mean.age, y = fitted)) +
  geom_line(color = "black", linewidth = 1) + 
  geom_line(aes(x = sig.deriv.ages, y = fitted), color = depth_colorbar[5], linewidth = 1) +
  theme_classic() +
  scale_y_continuous(limits = c(0.004, 0.007)) +
  xlab("Mean Age") + 
  ylab("R1 Rate of Change (predicted)\n") +
  theme(
  legend.position = "none",
  axis.text = element_text(size = 6, family = "Arial", color = c("black")),
  axis.title.x = element_text(size = 7, family ="Arial", color = c("black")),
  axis.title.y = element_text(size = 7, family ="Arial", color = c("black")),
  axis.line = element_line(linewidth = .2), 
  axis.ticks = element_line(linewidth = .2)) 

ggsave(filename = "/Volumes/Hera/Projects/corticalmyelin_development/Figures/Figure2_supplementaryb/R1slope_byage_trajectory.pdf", device = "pdf", dpi = 500, width = 2.07, height = 1.57)

Myelin Develops Heterochronously Across Cortical Depths

Number of significant regional effects at each depth

#Function to calculate the number of significant age effects post-FDR
ageeffect.results <- function(depth){
  ageeffects <- depth
  n_roi <- nrow(ageeffects)
 
  #significant smooth terms
  aageeffects <- ageeffects %>% mutate(significant = p.adjust(ageeffects$GAM.smooth.pvalue, method = c("fdr")) < 0.05)
  sig_age <- aageeffects %>% filter(significant == TRUE) %>% nrow()
  sig_age_percent <- round((sig_age/n_roi)*100, 2)
                         
  sprintf("%s percent of regions show a positive significant developmental change in R1", sig_age_percent)
}
lapply(gam.statistics.alldepths, function(depth){
  ageeffect.results(depth)
})
## $depth_1
## [1] "85.93 percent of regions show a positive significant developmental change in R1"
## 
## $depth_2
## [1] "92.59 percent of regions show a positive significant developmental change in R1"
## 
## $depth_3
## [1] "92.59 percent of regions show a positive significant developmental change in R1"
## 
## $depth_4
## [1] "95.56 percent of regions show a positive significant developmental change in R1"
## 
## $depth_5
## [1] "97.78 percent of regions show a positive significant developmental change in R1"
## 
## $depth_6
## [1] "98.52 percent of regions show a positive significant developmental change in R1"
## 
## $depth_7
## [1] "98.52 percent of regions show a positive significant developmental change in R1"

Regional derivatives at each depth

gam.derivatives.alldepths.long <- do.call(rbind, gam.derivatives.alldepths)
gam.derivatives.alldepths.long <- gam.derivatives.alldepths.long %>% mutate(depth = as.factor(substr(row.names(gam.derivatives.alldepths.long), 1, 7))) #add depth as a column
gam.derivatives.alldepths.long <- merge(gam.derivatives.alldepths.long, glasser.regions, sort = F)

#Calculate the average first derivative in each region at each depth
regional.rate.alldepths <- gam.derivatives.alldepths.long %>% group_by(label, depth) %>% do(rate = mean(.$derivative)) %>% unnest(cols = "rate")

#Calculate the average across-region first derivative at each depth
frontallobe.rate.alldepths <- regional.rate.alldepths %>% group_by(depth) %>% do(mean.rate = mean(.$rate)) %>% unnest(cols = "mean.rate")
frontallobe.rate.alldepths$depth <- factor(frontallobe.rate.alldepths$depth, ordered = T, levels = ordered_depths)

Depth-wise derivative plot

frontallobe.rate.alldepths %>% 
  ggplot(aes(x = mean.rate, y = depth, fill = depth)) +
  geom_point(shape = 23, size = 5.5, color = "white") +
  theme_classic() +
  scale_fill_manual(values = depth_colorbar) +
  xlab("\nMean Derivative") +
  ylab("Cortical Depth\n") +
  scale_x_continuous(breaks = c(0.0010, 0.0014, 0.0018), limits = c(0.00085, 0.0019)) +
  theme(
  legend.position = "none",
  axis.text = element_text(size = 6, family = "Arial", color = c("black")),
  axis.title.x = element_text(size = 7, family ="Arial", color = c("black")),
  axis.title.y = element_text(size = 7, family ="Arial", color = c("black")),
  axis.line = element_line(linewidth = .2), 
  axis.ticks = element_line(linewidth = .2)) 

ggsave(filename = "/Volumes/Hera/Projects/corticalmyelin_development/Figures/Figure2/Depthplot_derivative.pdf", device = "pdf", dpi = 500, width = 2.1, height = 4.0)

Depth-wise derivative brain plots

for(this.depth in c(unique(regional.rate.alldepths$depth))){
  
  regional.rate.depth <- regional.rate.alldepths %>% filter(depth == this.depth) %>% filter(label != "lh_L_10pp")
  
  derivative.plot <- ggplot() + 
  geom_brain(data = regional.rate.depth, atlas = glasser, mapping = aes(fill = rate), colour=I("gray20"), size=I(.08)) + theme_classic() + theme(legend.position = "none") + 
  paletteer::scale_fill_paletteer_c("grDevices::PuBuGn", direction = -1, limits = c(0.0005, 0.0022), oob = squish, na.value = "white") +  
  theme(legend.position = "none", axis.text = element_blank(), axis.line = element_blank(), axis.ticks = element_blank()) +
  new_scale_fill() + 
  geom_brain(data = glasser.plotting, atlas = glasser, mapping = aes(fill = cortex),  colour=I(alpha("gray20", 0)), size=I(0)) +
  scale_fill_manual(values = c(alpha("white", 0), "gray75")) 
  
  print(derivative.plot)
  
  ggsave(filename = sprintf("/Volumes/Hera/Projects/corticalmyelin_development/Figures/Figure2/%s_rate_corticalmap.pdf", this.depth), device = "pdf", dpi = 500, width = 3.95, height = 2)
}

Depth correlation matrix

regional.rate.alldepths.wide <- regional.rate.alldepths %>% pivot_wider(id_cols = label, names_from = depth, values_from = rate) %>% select(-label)
regional.rate.alldepths.wide <- regional.rate.alldepths.wide %>% select(depth_7, depth_6, depth_5, depth_4, depth_3, depth_2, depth_1) #order for plot
depth.derivative.cors <- cor(regional.rate.alldepths.wide) # compute correlation matrix

ggcorrplot(corr = depth.derivative.cors, method = "square", type = "upper", show.diag = T, lab = F) +
scale_fill_gradientn(colors = c("white", "#CCDCED", "#BFD2E2", "#7B9FC6"), limits = c(0.7, 1))

Coefficient of variation

rate.cov.bydepth <- data.frame(depth = factor(), cov = double())
for(this.depth in c(unique(regional.rate.alldepths$depth))){
  depth.data <- regional.rate.alldepths %>% filter(depth == this.depth)
  cov <- cv(depth.data$rate)
  cov <- data.frame(depth = this.depth, cov = cov)
  rate.cov.bydepth <- rbind(rate.cov.bydepth, cov)
}
rate.cov.bydepth
##     depth       cov
## 1 depth_1 0.5074685
## 2 depth_2 0.4501392
## 3 depth_3 0.4168052
## 4 depth_4 0.3802468
## 5 depth_5 0.3417924
## 6 depth_6 0.3086435
## 7 depth_7 0.2845120

Regional age of maturation at each depth

gam.statistics.alldepths.long <- do.call(rbind, gam.statistics.alldepths)
gam.statistics.alldepths.long <- gam.statistics.alldepths.long %>% mutate(depth = as.factor(substr(row.names(gam.statistics.alldepths.long), 1, 7))) #add depth as a column
regional.maturation.alldepths <- gam.statistics.alldepths.long
regional.maturation.alldepths <- merge(regional.maturation.alldepths, glasser.regions, sort = F)

#Calculate the average across-region age of maturation at each depth
frontallobe.maturation.alldepths <- regional.maturation.alldepths %>% group_by(depth) %>% do(mean.age = mean(.$smooth.increase.offset, na.rm = T)) %>% unnest(cols = "mean.age")
frontallobe.maturation.alldepths$depth <- factor(frontallobe.maturation.alldepths$depth, ordered = T, levels = ordered_depths)

Depth-wise age of maturation plot

frontallobe.maturation.alldepths %>% 
  ggplot(aes(x = mean.age, y = depth, fill = depth)) +
  geom_point(shape = 23, size = 5.5, color = "white") +
  theme_classic() +
  scale_fill_manual(values = depth_colorbar) +
  xlab("\nAge of Maturation") +
  ylab("Cortical Depth\n") +
  scale_x_continuous(limits = c(25.95, 30.5)) +
  theme(
  legend.position = "none",
  axis.text = element_text(size = 6, family = "Arial", color = c("black")),
  axis.title.x = element_text(size = 7, family ="Arial", color = c("black")),
  axis.title.y = element_text(size = 7, family ="Arial", color = c("black")),
  axis.line = element_line(linewidth = .2), 
  axis.ticks = element_line(linewidth = .2)) 

ggsave(filename = "/Volumes/Hera/Projects/corticalmyelin_development/Figures/Figure2/Depthplot_maturation.pdf", device = "pdf", dpi = 500, width = 2.1, height = 4.0)

Depth-wise age of maturation brain plots

for(this.depth in c(unique(regional.maturation.alldepths$depth))){
  
  regional.mat.depth <- regional.maturation.alldepths %>% filter(depth == this.depth) %>% filter(label != "lh_L_10pp")
  
  derivative.plot <- ggplot() + 
  geom_brain(data = regional.mat.depth, atlas = glasser, mapping = aes(fill = smooth.increase.offset), colour=I("gray20"), size=I(.08)) + theme_classic() + theme(legend.position = "none") + 
  paletteer::scale_fill_paletteer_c("grDevices::PuBuGn", direction = 1, limits = c(26, 34), oob = squish, na.value = "white") +  
  theme(legend.position = "none", axis.text = element_blank(), axis.line = element_blank(), axis.ticks = element_blank()) +
  new_scale_fill() + 
  geom_brain(data = glasser.plotting, atlas = glasser, mapping = aes(fill = cortex),  colour=I(alpha("gray50", 0)), size=I(0)) +
  scale_fill_manual(values = c(alpha("white", 0), "gray75")) 
  
  print(derivative.plot)
  
  ggsave(filename = sprintf("/Volumes/Hera/Projects/corticalmyelin_development/Figures/Figure2/%s_maturation_corticalmap.pdf", this.depth), device = "pdf", dpi = 500, width = 3.95, height = 2)
}

Depth correlation matrix

regional.maturation.alldepths.wide <- regional.maturation.alldepths %>% pivot_wider(id_cols = label, names_from = depth, values_from = smooth.last.change) %>% select(-label)
regional.maturation.alldepths.wide <- regional.maturation.alldepths.wide %>% select(depth_7, depth_6, depth_5, depth_4, depth_3, depth_2, depth_1) #order for plot
depth.maturation.cors <- cor(regional.maturation.alldepths.wide, use = "complete.obs") # compute correlation matrix

ggcorrplot(corr = depth.maturation.cors, method = "square", type = "upper", show.diag = T, lab = F) +
scale_fill_gradientn(colors = c("white", "#CCDCED", "#BFD2E2", "#7B9FC6"), limits = c(0.3, 1))

Coefficient of variation

maturation.cov.bydepth <- data.frame(depth = factor(), cov = double())
for(this.depth in c(unique(regional.rate.alldepths$depth))){
  depth.data <- regional.maturation.alldepths %>% filter(depth == this.depth)
  cov <- cv(depth.data$smooth.increase.offset, na.rm = T)
  cov <- data.frame(depth = this.depth, cov = cov)
  maturation.cov.bydepth <- rbind(maturation.cov.bydepth, cov)
}
maturation.cov.bydepth
##     depth       cov
## 1 depth_1 0.1297405
## 2 depth_2 0.1520745
## 3 depth_3 0.1417636
## 4 depth_4 0.1372049
## 5 depth_5 0.1365760
## 6 depth_6 0.1350223
## 7 depth_7 0.1286170

Frontal Myelin Matures Hierarchically Within Cortical Depths

map.colorbar <- c("#edf6ff", "#c2d4ed", "#809dc4", "#345c91", "#0e4669")

Variation in myelin development is patterned along the S-A axis

SA.frontal <- S.A.axis %>% filter(.$orig_parcelname %in% glasser.frontal$orig_parcelname,) %>% filter(!(.$orig_parcelname %in% glasser.snr.exclude$orig_parcelname)) %>% filter(label != "lh_L_10pp")

ggplot() + 
  geom_brain(data = SA.frontal, atlas = glasser, mapping = aes(fill = SA.axis), colour=I("gray20"), size=I(.08)) + theme_classic() + theme(legend.position = "none") + 
  scale_fill_gradientn(colors = map.colorbar, trans = 'reverse', limits = c(360, 60), oob = squish, na.value = "white") + 
  theme(legend.position = "none", axis.text = element_blank(), axis.line = element_blank(), axis.ticks = element_blank()) +
  new_scale_fill() + 
  geom_brain(data = glasser.plotting, atlas = glasser, mapping = aes(fill = cortex),  colour=I(alpha("gray50", 0)), size=I(0)) +
  scale_fill_manual(values = c(alpha("white", 0), "gray75")) 

ggsave(filename = "/Volumes/Hera/Projects/corticalmyelin_development/Figures/Figure3/SAaxis_corticalmap.pdf", device = "pdf", dpi = 500, width = 4.3, height = 2.2)
rate.SAaxis.bydepth <- data.frame(depth = factor(), SA.corr = double())
for(this.depth in c(unique(regional.rate.alldepths$depth))){
  depth.data <- regional.rate.alldepths %>% filter(depth == this.depth)
  depth.data <- merge(depth.data, S.A.axis)
  corr <- cor(depth.data$SA.axis, depth.data$rate, method = c("spearman"), use = "complete.obs")
  corr <- data.frame(depth = this.depth, SA.corr = corr)
  rate.SAaxis.bydepth <- rbind(rate.SAaxis.bydepth, corr)
}
rate.SAaxis.bydepth
##     depth    SA.corr
## 1 depth_1 -0.5096381
## 2 depth_2 -0.4888304
## 3 depth_3 -0.4633743
## 4 depth_4 -0.4102722
## 5 depth_5 -0.3581602
## 6 depth_6 -0.2967906
## 7 depth_7 -0.2408838
rate.SAaxis.bydepth$depth <- factor(rate.SAaxis.bydepth$depth, ordered = T, levels = ordered_depths)
ggplot(rate.SAaxis.bydepth, aes(x = SA.corr, y = depth,  fill = depth)) +
  geom_bar(stat = "identity") +
  theme_classic() +
  xlab("\nDevelopmental Correlation") +
  ylab("Cortical Depth\n") +
  scale_x_continuous(breaks = c(-0.5, -0.25, 0), limits = c(-0.52, 0)) +
  scale_fill_manual(values = depth_colorbar) +
  theme(axis.text.y = element_text(angle = 45, hjust = 1)) +
  theme(
        legend.position = "none", 
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        axis.text = element_text(size = 6, family = "Arial", color = c("black")),
        axis.title.x = element_text(size = 7, family ="Arial", color = c("black")),
        axis.title.y = element_text(size = 7, family ="Arial", color = c("black")),
        axis.line = element_line(linewidth = .2), 
        axis.ticks = element_line(linewidth = .2))

ggsave(filename = "/Volumes/Hera/Projects/corticalmyelin_development/Figures/Figure3/SAaxis_rate_depthcorrelation.pdf", device = "pdf", dpi = 500, width = 2.6, height = 1.7)
regional.rate.alldepths %>% filter(depth == "depth_1") %>% merge(., S.A.axis) %>% 
  ggplot(., aes(x = SA.axis, y = rate)) + geom_point(size = .5) +
  geom_smooth(method = "lm", linewidth = 1.5, color = depth_colorbar[7], fill = "gray78") +
  labs(x="\nSensorimotor-Association Axis", y=sprintf("Rate of Increase (derivative)\n")) +
  scale_y_continuous(breaks = c(0, 0.001, 0.002)) +
  theme_classic() +
    theme(
        legend.position = "none", 
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        axis.text = element_text(size = 6, family = "Arial", color = c("black")),
        axis.title.x = element_text(size = 7, family ="Arial", color = c("black")),
        axis.title.y = element_text(size = 7, family ="Arial", color = c("black")),
        axis.line = element_line(linewidth = .2), 
        axis.ticks = element_line(linewidth = .2))

ggsave(filename = "/Volumes/Hera/Projects/corticalmyelin_development/Figures/Figure3/SAaxis_rate_depth1.pdf", device = "pdf", dpi = 500, width = 2.6, height = 1.7)

Variation in myelin development aligns with cytoarchitectural variation

bigbrain.frontal <- bigbrain %>% filter(.$orig_parcelname %in% glasser.frontal$orig_parcelname,) %>% filter(!(.$orig_parcelname %in% glasser.snr.exclude$orig_parcelname)) %>% filter(label != "lh_L_10pp")

ggplot() + 
  geom_brain(data = bigbrain.frontal, atlas = glasser, mapping = aes(fill = cytoarchitecture.gradient), colour=I("gray20"), size=I(.08)) + theme_classic() + theme(legend.position = "none") + 
  scale_fill_gradientn(colors = map.colorbar, limits = c(-.3, .3), oob = squish, na.value = "white") + 
  theme(legend.position = "none", axis.text = element_blank(), axis.line = element_blank(), axis.ticks = element_blank()) +
  new_scale_fill() + 
  geom_brain(data = glasser.plotting, atlas = glasser, mapping = aes(fill = cortex),  colour=I(alpha("gray50", 0)), size=I(0)) +
  scale_fill_manual(values = c(alpha("white", 0), "gray75")) 
## merging atlas and data by 'label'
## merging atlas and data by 'label'

ggsave(filename = "/Volumes/Hera/Projects/corticalmyelin_development/Figures/Figure3/Cytoarchitecture_corticalmap.pdf", device = "pdf", dpi = 500, width = 4.3, height = 2.2)
## merging atlas and data by 'label'
## merging atlas and data by 'label'
rate.bigbrain.bydepth <- data.frame(depth = factor(), histology.corr = double())
for(this.depth in c(unique(regional.rate.alldepths$depth))){
  depth.data <- regional.rate.alldepths %>% filter(depth == this.depth)
  depth.data <- merge(depth.data, bigbrain)
  corr <- cor(depth.data$cytoarchitecture.gradient, depth.data$rate, method = c("spearman"), use = "complete.obs")
  corr <- data.frame(depth = this.depth, histology.corr = corr)
  rate.bigbrain.bydepth <- rbind(rate.bigbrain.bydepth, corr)
}
rate.bigbrain.bydepth
##     depth histology.corr
## 1 depth_1      0.6371086
## 2 depth_2      0.6854697
## 3 depth_3      0.6813725
## 4 depth_4      0.6503414
## 5 depth_5      0.6208272
## 6 depth_6      0.5783826
## 7 depth_7      0.5435518
rate.bigbrain.bydepth$depth <- factor(rate.bigbrain.bydepth$depth, ordered = T, levels = ordered_depths)
ggplot(rate.bigbrain.bydepth, aes(x = histology.corr, y = depth,  fill = depth)) +
  geom_bar(stat = "identity") +
  theme_classic() +
  xlab("\nCytoarchitectural Differentiation Correlation") +
  ylab("Cortical Depth\n") +
  scale_x_continuous(breaks = c(0, 0.35, 0.7)) +
  scale_fill_manual(values = depth_colorbar) +
  theme(axis.text.y = element_text(angle = 45, hjust = 1)) +
  theme(
        legend.position = "none", 
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        axis.text = element_text(size = 6, family = "Arial", color = c("black")),
        axis.title.x = element_text(size = 7, family ="Arial", color = c("black")),
        axis.title.y = element_text(size = 7, family ="Arial", color = c("black")),
        axis.line = element_line(linewidth = .2), 
        axis.ticks = element_line(linewidth = .2))

ggsave(filename = "/Volumes/Hera/Projects/corticalmyelin_development/Figures/Figure3/Cytoarchitecture_rate_depthcorrelation.pdf", device = "pdf", dpi = 500, width = 2.6, height = 1.7)
regional.rate.alldepths %>% filter(depth == "depth_1") %>% merge(., bigbrain) %>% 
  ggplot(., aes(x = cytoarchitecture.gradient, y = rate)) + geom_point(size = .5) +
  geom_smooth(method = "lm", linewidth = 1.5, color = depth_colorbar[7], fill = "gray78") +
  labs(x="\nCytoarchitectural Variation", y=sprintf("Rate of Increase (derivative)\n")) +
  scale_y_continuous(breaks = c(0, 0.001, 0.002)) +
  theme_classic() +
    theme(
        legend.position = "none", 
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        axis.text = element_text(size = 6, family = "Arial", color = c("black")),
        axis.title.x = element_text(size = 7, family ="Arial", color = c("black")),
        axis.title.y = element_text(size = 7, family ="Arial", color = c("black")),
        axis.line = element_line(linewidth = .2), 
        axis.ticks = element_line(linewidth = .2))

ggsave(filename = "/Volumes/Hera/Projects/corticalmyelin_development/Figures/Figure3/Cytoarchitecture_rate_depth1.pdf", device = "pdf", dpi = 500, width = 2.6, height = 1.7)

Depth-wise Maturational Profiles Reflect Functional Diversity

Depth-dependent trajectories in exemplar frontal regions

gam.smoothestimates.alldepths.long <- do.call(rbind, gam.smoothestimates.alldepths)
gam.smoothestimates.alldepths.long <- gam.smoothestimates.alldepths.long %>% mutate(depth = as.factor(substr(row.names(gam.smoothestimates.alldepths.long), 1, 7))) #add depth as a column
gam.smoothestimates.alldepths.long$depth <- factor(gam.smoothestimates.alldepths.long$depth, ordered = T, levels = ordered_depths)
#Function to plot age smooth functions in exemplar regions
plot.depth.smooths <- function(region, y_ticks){

  gam.smoothestimates.alldepths.long$depth <- factor(gam.smoothestimates.alldepths.long$depth, ordered = T, levels = depth.list)

  smooth.plot <- gam.smoothestimates.alldepths.long %>% filter(orig_parcelname == region) %>%
  ggplot(., aes(x = age, y = est, group = depth, color = depth)) +
  geom_line(linewidth = .4) +
  theme_classic() +
  xlab("\nAge (years)") +
  ylab("R1 Trajectory (zero-centered)\n") +
  scale_y_continuous(breaks = y_ticks) +
  scale_color_manual(values = depth_colorbar_reverse) +
  theme(
        legend.position = "none", 
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        axis.text = element_text(size = 6, family = "Arial", color = c("black")),
        axis.title.x = element_text(size = 7, family ="Arial", color = c("black")),
        axis.title.y = element_text(size = 7, family ="Arial", color = c("black")),
        axis.line = element_line(linewidth = .2), 
        axis.ticks = element_line(linewidth = .2))
  
  return(smooth.plot)
}

Primary Motor (4)

glasser.regions %>% filter(orig_parcelname == "L_4_ROI") %>%
ggseg(.data = ., atlas = "glasser", mapping=aes(fill = orig_parcelname, colour=I("gray25"), size=I(.07)), position = c("stacked"), hemisphere = "left", view = "lateral") + 
  theme_void() + 
  labs(fill="") +
  scale_fill_manual(values = c("#395F94"), na.value = "white") +
  theme(legend.position = "none")
## merging atlas and data by 'label'

ggsave(filename = "/Volumes/Hera/Projects/corticalmyelin_development/Figures/Figure4/Area4_corticalmap.pdf", device = "pdf", dpi = 500, width = .75, height = .55)
plot.depth.smooths(region = "L_4_ROI", y_ticks = c(-0.04, -0.02, 0, 0.02))

ggsave(filename = "/Volumes/Hera/Projects/corticalmyelin_development/Figures/Figure4/Area4_depthsmooths.pdf", device = "pdf", dpi = 500, width = 2.32, height = 1.82)

Dorsolateral PFC (46)

glasser.regions %>% filter(orig_parcelname == "L_46_ROI") %>%
ggseg(.data = ., atlas = "glasser", mapping=aes(fill = orig_parcelname, colour=I("gray25"), size=I(.07)), position = c("stacked"), hemisphere = "left", view = "lateral") + 
  theme_void() + 
  labs(fill="") +
  scale_fill_manual(values = c("#395F94"), na.value = "white") +
  theme(legend.position = "none")
## merging atlas and data by 'label'

ggsave(filename = "/Volumes/Hera/Projects/corticalmyelin_development/Figures/Figure4/Area46_corticalmap.pdf", device = "pdf", dpi = 500, width = .75, height = .55)
plot.depth.smooths(region = "L_46_ROI", y_ticks = c(-0.02, -0.01, 0, 0.01))

ggsave(filename = "/Volumes/Hera/Projects/corticalmyelin_development/Figures/Figure4/Area46_depthsmooths.pdf", device = "pdf", dpi = 500, width = 2.32, height = 1.82)

Anterior Cingulate (a24)

glasser.regions %>% filter(orig_parcelname == "L_a24_ROI") %>%
ggseg(.data = ., atlas = "glasser", mapping=aes(fill = orig_parcelname, colour=I("gray25"), size=I(.07)), position = c("stacked"), hemisphere = "left", view = "medial") + 
  theme_void() + 
  labs(fill="") +
  scale_fill_manual(values = c("#395F94"), na.value = "white") +
  theme(legend.position = "none")
## merging atlas and data by 'label'

ggsave(filename = "/Volumes/Hera/Projects/corticalmyelin_development/Figures/Figure4/Areaa24_corticalmap.pdf", device = "pdf", dpi = 500, width = .75, height = .55)
plot.depth.smooths(region = "L_a24_ROI", y_ticks = c(-0.01, 0, 0.01))

ggsave(filename = "/Volumes/Hera/Projects/corticalmyelin_development/Figures/Figure4/Areaa24_depthsmooths.pdf", device = "pdf", dpi = 500, width = 2.32, height = 1.83)

Developmental trajectory curvatures

#A function to compute the curvature of the estimated trajectory
curvature <- function(df, x,  y){
age.spline <- with(df, smooth.spline(x, y, df = 10))
first.deriv <- with(df, predict(age.spline, x = x, deriv = 1))
second.deriv <- with(df, predict(age.spline, x = x, deriv = 2))

k <- (second.deriv$y / ((1 + (first.deriv$y^2))^(3/2)))
k <- abs(k)
return(k)}
#Calculate average curvature of R1 growth trajectories for every region at every cortical depth
trajectory.curvatures <- map_dfr(unique(gam.statistics.alldepths.long$orig_parcelname), function(r){
  region.smooths <- gam.smoothestimates.alldepths.long %>% filter(orig_parcelname == r) #smooth fits for this region at all depths
  depth.curvature <- region.smooths %>% group_by(depth) %>% do(curvature = mean(curvature(df = ., x = .$age , y = .$est))) %>% unnest(cols = c("curvature")) #calculate curvature of the smooth function
  depth.curvature$orig_parcelname <- r
  return(depth.curvature)
})

trajectory.curvatures <- merge(trajectory.curvatures, glasser.regions)
trajectory.curvatures.wide <- trajectory.curvatures %>% pivot_wider(id_cols = c("label"), names_from = "depth", values_from = "curvature") 
labels <- trajectory.curvatures.wide$label
trajectory.curvatures.wide <- trajectory.curvatures.wide %>% dplyr::select(-label)

Assess cluster-ability

library(factoextra)
library("NbClust")
#Assess clustering tendency before applying clustering!
##To assess the clustering tendency, the Hopkins’ statistic is used. It measures the probability that data are generated by a uniform data distribution 
##If the value of Hopkins statistic is close to 1 (far above 0.5), then we can conclude that the dataset is significantly clusterable.

(trajectory.curvatures.wide %>% get_clust_tendency(n = nrow(trajectory.curvatures.wide)-1, seed = 123))$hopkins_stat #hopkins stat + dissimilarity matrix image confirm data is clusterable 
## [1] 0.8888908

Determine the optimal number of clusters from a variety of approaches

set.seed(123)

#Use NbClust to determine the best number of clusters to use. This package compares 30 different indexes for determining cluster number and summarizes the best option across all 30

pdf(file = NULL) #to suppress unnescessary plotting

res.nbclust <- trajectory.curvatures.wide %>% scale() %>%
  NbClust(distance = "euclidean",
          min.nc = 2, max.nc = 8, 
          method = "kmeans", index ="all") 
## *** : The Hubert index is a graphical method of determining the number of clusters.
##                 In the plot of Hubert index, we seek a significant knee that corresponds to a 
##                 significant increase of the value of the measure i.e the significant peak in Hubert
##                 index second differences plot. 
## 
## *** : The D index is a graphical method of determining the number of clusters. 
##                 In the plot of D index, we seek a significant knee (the significant peak in Dindex
##                 second differences plot) that corresponds to a significant increase of the value of
##                 the measure. 
##  
## ******************************************************************* 
## * Among all indices:                                                
## * 5 proposed 2 as the best number of clusters 
## * 10 proposed 3 as the best number of clusters 
## * 1 proposed 4 as the best number of clusters 
## * 2 proposed 6 as the best number of clusters 
## * 1 proposed 7 as the best number of clusters 
## * 4 proposed 8 as the best number of clusters 
## 
##                    ***** Conclusion *****                            
##  
## * According to the majority rule, the best number of clusters is  3 
##  
##  
## *******************************************************************
dev.off()
## quartz_off_screen 
##                 2
#And confirm with a scree plot 
set.seed(123)
n_clusters <- 8
wss <- numeric(n_clusters)

#loop over 1 to n possible clusters
for (i in 1:n_clusters) {
  km.out <- kmeans(trajectory.curvatures.wide, centers = i)
  wss[i] <- km.out$tot.withinss
}
wss_df <- tibble(clusters = 1:n_clusters, wss = wss)

ggplot(wss_df, aes(x = clusters, y = wss, group = 1)) +
geom_line(linewidth = .8, color = depth_colorbar[4]) +
geom_point(size = 1) +
  theme_classic() +
  xlab("\nCluster Number") +
  ylab("Within-cluster SS\n") +
  scale_x_continuous(breaks = c(1, 2, 3, 4, 5, 6,7, 8)) +
  theme(
        legend.position = "none", 
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        axis.text.y = element_blank(),
        axis.text.x = element_text(size = 6, family = "Arial", color = c("black")),
        axis.title.x = element_text(size = 7, family ="Arial", color = c("black")),
        axis.title.y = element_text(size = 7, family ="Arial", color = c("black")),
        axis.line = element_line(linewidth = .2), 
        axis.ticks.y = element_blank(),
        axis.ticks.x = element_line(linewidth = .2))

ggsave(filename = "/Volumes/Hera/Projects/corticalmyelin_development/Figures/Figure4/Scree_plot.pdf", device = "pdf", dpi = 500, width = 1.32, height = 1.47)

Clusters

#Cluster into 3 and get cluster assignments
set.seed(123) #1 2 3 clusters for me
kmeans_clusters <- kmeans(trajectory.curvatures.wide, centers = 3)
clusters <- kmeans_clusters$cluster

kmeans.cluster.results <- data.frame(label = labels, cluster = as.factor(clusters))
 ggplot() + 
   geom_brain(data = kmeans.cluster.results, atlas = glasser, mapping = aes(fill = cluster), colour=I("gray10"), size=I(.08)) + theme_classic() +
 scale_fill_manual(values = c("#96bfd6", "#c0b6d4", "#275887"), na.value = "white") +  
  theme(legend.position = "none", axis.text = element_blank(), axis.line = element_blank(), axis.ticks = element_blank()) +
  new_scale_fill() + 
  geom_brain(data = glasser.plotting, atlas = glasser, mapping = aes(fill = cortex),  colour=I(alpha("gray50", 0)), size=I(0)) +
  scale_fill_manual(values = c(alpha("white", 0), "gray75")) 
## merging atlas and data by 'label'
## Warning: Some data not merged properly. Check for naming errors in data:
##   atlas type  hemi  region side  label           geometry cluster
##   <chr> <chr> <chr> <chr>  <chr> <chr>     <MULTIPOLYGON> <fct>  
## 1 <NA>  <NA>  <NA>  <NA>   <NA>  lh_L_10pp          EMPTY 2
## Warning: Some data not merged. Check for spelling mistakes in:
##          label cluster
## 396 lh_L_10pp       2
## merging atlas and data by 'label'

ggsave(filename = "/Volumes/Hera/Projects/corticalmyelin_development/Figures/Figure4/Clusters_corticalmap.pdf", device = "pdf", dpi = 500, width = 4, height = 2)
## merging atlas and data by 'label'
## Warning: Some data not merged properly. Check for naming errors in data:
##   atlas type  hemi  region side  label           geometry cluster
##   <chr> <chr> <chr> <chr>  <chr> <chr>     <MULTIPOLYGON> <fct>  
## 1 <NA>  <NA>  <NA>  <NA>   <NA>  lh_L_10pp          EMPTY 2
## Warning: Some data not merged. Check for spelling mistakes in:
##          label cluster
## 396 lh_L_10pp       2
## merging atlas and data by 'label'

Neurosynth Decoding

neurosynth.terms.clusters <- merge(neurosynth.terms, kmeans.cluster.results, by = c("label"))

clusters.neurosynth <- ldply(neurosynth.termlist, function(t){
  decode.df <- neurosynth.terms.clusters %>% group_by(cluster) %>% do(term.mean = mean(.[[t]])) %>% unnest(term.mean)
  decode.df$term <- t
  return(decode.df)})
clusters.neurosynth %>% filter(cluster == 1) %>% slice_max(n = 5, order_by = term.mean) %>%
ggplot(., aes(x = term.mean, y = term)) +
  geom_segment(aes(y = reorder(term, term.mean), yend = term, x = 0, xend =  
                     term.mean), color = "gray75", linewidth = .5) +
  geom_point(size = 2, color = "#96bfd6") +
  xlab("\nNeurosynth Term Score (z)") +
  ylab("") +
  theme_classic() +
  theme(
        legend.position = "none", 
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        axis.text = element_text(size = 8, family = "Arial", color = c("black")),
        axis.text.y = element_text(angle = 30, hjust = 1),
        axis.title.x = element_text(size = 7, family ="Arial", color = c("black")),
        axis.title.y = element_text(size = 7, family ="Arial", color = c("black")),
        axis.line = element_line(linewidth = .2), 
        axis.ticks = element_line(linewidth = .2))

clusters.neurosynth %>% filter(cluster == 3) %>% slice_max(n = 5, order_by = term.mean) %>%
ggplot(., aes(x = term.mean, y = term)) +
  geom_segment(aes(y = reorder(term, term.mean), yend = term, x = 0, xend =  
                     term.mean), color = "gray75", linewidth = .5) +
  geom_point(size = 2, color = "#275887") +
  xlab("\nNeurosynth Term Score (z)") +
  ylab("") +
  theme_classic() +
  theme(
        legend.position = "none", 
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        axis.text = element_text(size = 8, family = "Arial", color = c("black")),
        axis.text.y = element_text(angle = 35, hjust = 1),
        axis.title.x = element_text(size = 7, family ="Arial", color = c("black")),
        axis.title.y = element_text(size = 7, family ="Arial", color = c("black")),
        axis.line = element_line(linewidth = .2), 
        axis.ticks = element_line(linewidth = .2))

clusters.neurosynth %>% filter(cluster == 2) %>% slice_max(n = 5, order_by = term.mean) %>%
ggplot(., aes(x = term.mean, y = term)) +
  geom_segment(aes(y = reorder(term, term.mean), yend = term, x = 0, xend =  
                     term.mean), color = "gray75", linewidth = .5) +
  geom_point(size = 2, color = "#c0b6d4") +
  xlab("\nNeurosynth Term Score (z)") +
  ylab("") +
  scale_x_continuous(breaks = c(0, 0.5, 1)) +
  theme_classic() +
  theme(
        legend.position = "none", 
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        axis.text = element_text(size = 8, family = "Arial", color = c("black")),
        axis.text.y = element_text(angle = 30, hjust = 1),
        axis.title.x = element_text(size = 7, family ="Arial", color = c("black")),
        axis.title.y = element_text(size = 7, family ="Arial", color = c("black")),
        axis.line = element_line(linewidth = .2), 
        axis.ticks = element_line(linewidth = .2))

S-A Axis ranks

kmeans.cluster.results <- merge(kmeans.cluster.results, S.A.axis, by = "label", sort = F)
kmeans.cluster.results %>% group_by(cluster) %>% do(mean.SA = mean(.$SA.axis)) %>% unnest(cols = "mean.SA")
## # A tibble: 3 × 2
##   cluster mean.SA
##   <fct>     <dbl>
## 1 1          148.
## 2 2          278.
## 3 3          241.

Average cluster curvature across depths

cluster.curvatures <- merge(trajectory.curvatures, kmeans.cluster.results, by = c("label")) #curvature for clusters

#Compute average curvature across all regions in a cluster for each cortical depth
cluster.curvatures <- cluster.curvatures %>% group_by(cluster, depth) %>% do(average.curvature = mean(.$curvature)) %>% unnest(cols = average.curvature)
cluster.curvatures$average.curvature.z <- scale(cluster.curvatures$average.curvature, scale = T, center = F)[,1] #scale curvature for ease of plotting. scale divides each data point by the SD
cluster.curvatures$depth <- factor(cluster.curvatures$depth, ordered = T, levels = ordered_depths)
cluster.curvatures %>% 
   ggplot(aes(x = average.curvature.z, y = depth, group = cluster, color = cluster)) +
  geom_point(size = 1.25) +
  geom_path(size = .5) +
  theme_classic() +
  scale_color_manual(values = c("#96bfd6", "#c0b6d4", "#275887"), na.value = "white") +
  xlab("\nCurvature (scaled)") +
  ylab("Cortical Depth\n") +
  theme(
  legend.position = "none",
  axis.text = element_text(size = 6, family = "Arial", color = c("black")),
  axis.title.x = element_text(size = 7, family ="Arial", color = c("black")),
  axis.title.y = element_text(size = 7, family ="Arial", color = c("black")),
  axis.line = element_line(linewidth = .2), 
  axis.ticks = element_line(linewidth = .2)) 
## Warning: Using `size` aesthetic for lines was deprecated in ggplot2 3.4.0.
## ℹ Please use `linewidth` instead.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.

ggsave(filename = "/Volumes/Hera/Projects/corticalmyelin_development/Figures/Figure4/Depthplot_curvature.pdf", device = "pdf", dpi = 500, width = 2.6, height = 1.6)

Average cluster age of maturation across depths

#calculate average age of maturation at each depth, in each cluster
regional.maturation.alldepths <- merge(regional.maturation.alldepths, kmeans.cluster.results, by = "label", sort = F)
cluster.maturation <-  regional.maturation.alldepths %>% group_by(cluster, depth) %>% do(average.maturation = mean(.$smooth.increase.offset, na.rm = T)) %>% unnest(cols = c("average.maturation"))
cluster.maturation$depth <- factor(cluster.maturation$depth, ordered = T, levels = ordered_depths)
cluster.maturation$cluster.order[cluster.maturation$cluster == 1] <- "a"
## Warning: Unknown or uninitialised column: `cluster.order`.
cluster.maturation$cluster.order[cluster.maturation$cluster == 3] <- "b"
cluster.maturation$cluster.order[cluster.maturation$cluster == 2] <- "c"

cluster.maturation %>% 
  ggplot(aes(y = average.maturation, ymin = 20, x = depth, ymax = average.maturation, group = cluster.order, color = cluster.order)) +
  geom_linerange(position = position_dodge(1), linewidth = 1) +
  scale_y_continuous(breaks = c(20, 22, 24, 26, 28, 30, 32)) +
  ylab("\nAge of Maturation") +
  xlab("Cortical Depth\n") +
  coord_flip() +
  theme_classic() +
  scale_color_manual(values = c("#96bfd6", "#275887", "#c0b6d4"), na.value = "white") +
  theme(
  legend.position = "none",
  axis.text = element_text(size = 6, family = "Arial", color = c("black")),
  axis.title.x = element_text(size = 7, family ="Arial", color = c("black")),
  axis.title.y = element_text(size = 7, family ="Arial", color = c("black")),
  axis.line = element_line(linewidth = .2), 
  axis.ticks = element_line(linewidth = .2)) 

ggsave(filename = "/Volumes/Hera/Projects/corticalmyelin_development/Figures/Figure4/Depthplot_maturation.pdf", device = "pdf", dpi = 500, width = 2.95, height = 1.6)

Curvature variation

trajectory.curvatures$curvature.scaled <- scale(trajectory.curvatures$curvature, scale = T, center = F)
SG.curvature <- trajectory.curvatures %>% filter(depth == "depth_1" | depth == "depth_2" | depth == "depth_3") %>% group_by(label) %>% do(SG.curvature = mean(.$curvature.scaled)) %>% unnest(cols = c("SG.curvature")) #average superficial depths curvature
IG.curvature <- trajectory.curvatures %>% filter(depth == "depth_4" | depth == "depth_5" | depth == "depth_6" | depth == "depth_7") %>% group_by(label) %>% do(IG.curvature = mean(.$curvature.scaled)) %>% unnest(cols = c("IG.curvature")) #average deep depths curaature

SGIG.curvature <- merge(SG.curvature, IG.curvature, by = "label")
SGIG.curvature <- SGIG.curvature %>% mutate(curvature.difference = IG.curvature - SG.curvature) #difference between the two
plot.colorbar <- c("#edf6ff", "#c2d4ed", "#809dc4", "#345c91", "#0e4669")

ggplot() + 
  geom_brain(data = SGIG.curvature, atlas = glasser, mapping = aes(fill = curvature.difference), colour=I("gray25"), size=I(.08)) + 
  theme_classic() + 
  scale_fill_gradientn(colors = plot.colorbar, limits = c(0.4, 1), oob = squish, na.value = "white") +
  theme(legend.position = "none", axis.text = element_blank(), axis.line = element_blank(), axis.ticks = element_blank()) +
  new_scale_fill() + 
  geom_brain(data = glasser.plotting, atlas = glasser, mapping = aes(fill = cortex),  colour=I(alpha("gray50", 0)), size=I(0)) +
  scale_fill_manual(values = c(alpha("white", 0), "gray75")) 
## merging atlas and data by 'label'
## Warning: Some data not merged properly. Check for naming errors in data:
##   atlas type  hemi  region side  label          geometry SG.curvature
##   <chr> <chr> <chr> <chr>  <chr> <chr>    <MULTIPOLYGON>        <dbl>
## 1 <NA>  <NA>  <NA>  <NA>   <NA>  lh_L_10…          EMPTY     0.000345
## # ℹ 2 more variables: IG.curvature <dbl>, curvature.difference <dbl>
## Warning: Some data not merged. Check for spelling mistakes in:
##          label SG.curvature IG.curvature curvature.difference
## 396 lh_L_10pp 0.0003452231    0.8103573            0.8100121
## merging atlas and data by 'label'

ggsave(filename = "/Volumes/Hera/Projects/corticalmyelin_development/Figures/Figure4/Curvaturevariation_corticalmap.pdf", device = "pdf", dpi = 500, width = 4.1, height = 2)
## merging atlas and data by 'label'
## Warning: Some data not merged properly. Check for naming errors in data:
##   atlas type  hemi  region side  label          geometry SG.curvature
##   <chr> <chr> <chr> <chr>  <chr> <chr>    <MULTIPOLYGON>        <dbl>
## 1 <NA>  <NA>  <NA>  <NA>   <NA>  lh_L_10…          EMPTY     0.000345
## # ℹ 2 more variables: IG.curvature <dbl>, curvature.difference <dbl>
## Warning: Some data not merged. Check for spelling mistakes in:
##          label SG.curvature IG.curvature curvature.difference
## 396 lh_L_10pp 0.0003452231    0.8103573            0.8100121
## merging atlas and data by 'label'